<template>
  <a-modal
    :open="open"
    width="640px"
    :title="`${isEdit ? '编辑' : '新增'}${['处置套餐', '处方套餐', '协定方', '就诊套餐', '病历模板'][templateType]}`"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form :model="formState" class="form-style-flex">
      <a-form-item v-if="templateType == 4" label="病历分类" class="w-full">
        {{ mrmName }}
      </a-form-item>
      <a-form-item :label="templateType == 4 ? '模板类别' : '套餐类别'" class="w-1/2">
        <a-radio-group v-model:value="formState.sylb">
          <a-radio :value="0">个人</a-radio>
          <a-radio :value="1">科室</a-radio>
          <a-radio :value="2">全院</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item :label="templateType == 4 ? '模板名称' : '套餐名称'" v-bind="validateInfos.mc" class="w-full">
        <a-input v-model:value="formState.mc" placeholder="请输入" />
      </a-form-item>
      <a-form-item v-if="templateType !== 4 && !isArmy" label="开启套餐价" class="w-1/4">
        <a-switch v-model:checked="formState.enablePackagePricing" :un-checked-value="0" :checked-value="1" />
      </a-form-item>
      <a-form-item v-if="templateType !== 4 && !isArmy" label="套餐价格" v-bind="validateInfos.tcPrice" class="w-3/4">
        <a-input v-model:value="formState.tcPrice" v-input:float :disabled="!formState.enablePackagePricing" placeholder="请输入" />
      </a-form-item>
      <a-form-item v-if="templateType !== 4 && !isArmy" label="挂号套餐" class="w-1/4">
        <a-switch v-model:checked="formState.isRegTc" :un-checked-value="0" :checked-value="1" />
      </a-form-item>
      <a-form-item label="顺序号" v-bind="validateInfos.outOrder" class="w-3/4">
        <a-input v-model:value="formState.outOrder" placeholder="请输入" />
      </a-form-item>
      <a-form-item v-if="templateType === 3" label="就诊事项" class="w-full">
        <a-select v-model:value="formState.dictOutpMatterId" placeholder="请选择就诊事项" allow-clear mode="multiple" :max-tag-count="4">
          <a-select-option v-for="(item, index) in dictOutList" :key="index" :value="item.dictOutpMatterId">{{ item.matterName }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :label="templateType == 4 ? '模板说明' : '套餐说明'" class="w-full">
        <a-textarea v-model:value="formState.yzsm" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  mrmCode: {
    type: String,
    default: null
  },
  rowData: {
    type: Object,
    default: () => ({})
  },
  templateType: {
    description: '模板类型 0-处置 1-处方 2-协定方 3-就诊套餐 4-病历',
    type: Number,
    default: 0
  },
  mrmName: {
    description: '模板病例分类',
    type: String,
    default: ''
  }
})
const dictOutList = ref([])
const formState = ref({
  sylb: 0,
  mc: '',
  yzsm: '',
  tcPrice: ''
})
const rulesState = ref({
  mc: [{ required: true, message: '请选择套餐名称' }]
})
const { isCompleted, resetFields, validate, validateInfos } = useForm(formState, rulesState)
const isEdit = ref(false)
watch(
  () => props.open,
  (val) => {
    props.templateType === 3 && getDictOutList()
    if (val && Object.keys(props.rowData).length) {
      isEdit.value = true
      formState.value = Object.assign({}, { ...props.rowData, dictOutpMatterId: props.rowData?.dictOutpMatterId?.split(',') })
    } else {
      isEdit.value = false
      formState.value = {
        sylb: 0,
        mc: '',
        yzsm: '',
        tcPrice: ''
      }
    }
  }
)
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}
const onOk = async () => {
  await validate()
  const { code, msg } = await post('/outp/template/doAddClinicalOrderPackage', {
    ...formState.value,
    tclb: 0,
    mrmCode: props.mrmCode,
    czcf: props.templateType,
    tcPrice: formState.value.tcPrice || 0,
    enablePackagePricing: formState.value.enablePackagePricing ? 1 : 0,
    isRegTc: formState.value.isRegTc ? 1 : 0,
    dictOutpMatterId: props.templateType === 3 ? formState.value.dictOutpMatterId?.join(',') : undefined
  })
  if (code === 0) {
    message.success('保存成功')
    emit('success')
    onCancel()
  } else {
    message.error(msg)
  }
}

// 获取就诊事项
const getDictOutList = async () => {
  const res = await post('/outp/mzdj/getDictOutpMatterNumberList')
  if (res.code !== 0) return message.error(res.msg)
  dictOutList.value = res.data
  if (!props.rowData?.tcid) {
    formState.value.dictOutpMatterId = res.data?.map((item) => item.dictOutpMatterId)
  } else {
    formState.value = Object.assign({}, { ...props.rowData, dictOutpMatterId: props.rowData?.dictOutpMatterId?.split(',') })
  }
}
const isArmy = getSystemConfig('NAVYHIS_MODE') === '1' //军用系统
</script>
